home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7731 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: need for function prototypes
  5. Date: 27 Feb 1996 18:59:33 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb27115933@qcd.lanl.gov>
  8. References: <4gutho$o1a@mn5.swip.net>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: chris.rossall@mailbox.swipnet.se's message of Tue, 27 Feb 1996 12:45:14 GMT
  13.  
  14. In article <4gutho$o1a@mn5.swip.net>
  15. chris.rossall@mailbox.swipnet.se (Chris Rossall) writes:
  16.  
  17. CR: Hello I am having trouble understanding why I should use function
  18. CR: prototypes. I mean,if the function is defined before it is used,the
  19. CR: compiler should have enough information about the parameters to
  20. CR: produce correct code anyway.
  21.  
  22. Point 1:
  23.  
  24. The calling sequence of a function with a prototype is often different
  25. from the type it seems to be declared with. Thus in
  26.  
  27. int f(x)
  28. char x;
  29. { /* definition */ }
  30.  
  31. The calling sequence actually passes an int. So, if you wrote, 
  32.  
  33. char a;
  34. f(a);
  35.  
  36. The compiler would have to change a to an int before passing. This it
  37. does automatically, if it does not find a prototype.
  38.  
  39. On the other hand, 
  40.  
  41. int f(char x) { /* definition */ }
  42.  
  43. needs x to be passed as a char. So, this time, before you write
  44.  
  45. char a;
  46. f(a);
  47.  
  48. you must make sure that the prototype is visible, so that the compiler
  49. does not change a to an int before passing.
  50.  
  51. Point 2: There is no way to use the calling convention of a variable
  52. argument function (e.g. printf) without a prototype.
  53.  
  54. Point 3: Mistakes happen to everyone. A compilers duty is to check for
  55. stupid user errors. Correctly used prototypes specifically ask the
  56. compiler to check for user errors.
  57.  
  58. Point 4: When one uses a prototype, some automatic conversions take
  59. place. These are useful. Thus, after `int f(char *x);' f(NULL) means
  60. f((char*)NULL). In fact use of uncast NULL as a parameter to a
  61. non-prototyped function is always a mistake.
  62.  
  63. Cheers
  64. Tanmoy
  65. --
  66. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  67. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  68. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  69. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  70. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  71. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  72.